﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public partial class DispatchSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            string sql = @"SELECT A.*, B.Completed AS FlowCompleted FROM dbo.t5715_Dispatch A 
                                LEFT JOIN dbo.FlowInstance B ON A.FlowInstanceID = B.FlowInstanceID
                                WHERE DispatchState <> 1
                                ORDER BY A.CreateDate DESC";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.CommandName) || e.CommandName.ToLower().Equals("page"))
            {
                return;
            }

            GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
            string flowInstanceID = gridView.DataKeys[gridViewRow.RowIndex]["FlowInstanceID"].ToString();           

            switch (e.CommandName)
            { 
                case "CmdSelect" :

                    break;                
                default:
                    break;
            }
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string flowID = gridView.DataKeys[e.Row.RowIndex]["FlowID"].ToString();
                string flowInstanceID = gridView.DataKeys[e.Row.RowIndex]["FlowInstanceID"].ToString();

                TableCell tableCell_DispatchState = e.Row.Cells[9];
                string dispatchState = tableCell_DispatchState.Text;
                switch (dispatchState)
                {
                    case "1":
                        tableCell_DispatchState.Text = "已完成";
                        break;
                    default:
                        tableCell_DispatchState.Text = "未完成";
                        break;
                }

                TableCell tableCell_FlowCompleted = e.Row.Cells[10];
                string flowCompleted = tableCell_FlowCompleted.Text.ToLower();
                switch (flowCompleted)
                { 
                    case "true" :
                        tableCell_FlowCompleted.Text = "已完成";
                        e.Row.BackColor = System.Drawing.Color.LightGreen;
                        break;
                    case "false" :
                        tableCell_FlowCompleted.Text = "处理中";
                        e.Row.BackColor = System.Drawing.Color.Pink;
                        break;
                    default:
                        tableCell_FlowCompleted.Text = "无";
                        break;
                }                                            
            }
        }
    }
}